package com.example.text.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.common.result.Result;
import com.example.text.common.result.ResultFactory;
import com.example.text.entity.ExamineEntity;
import com.example.text.service.ExamineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

@RestController
public class ExamineController {

    @Autowired
    ExamineService examineService;
    //显示采购审批表
    @RequiresPermissions("/api/admin/purchase")
    @GetMapping("/api/admin/purchase/{size}/{page}")
    public Result listPurchase(@PathVariable("size") int size, @PathVariable("page") int page) throws Exception {
        System.out.println("一页有："+size+"个数据");
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(examineService.selectExaminePage(pages,1));
    }

   //根据编号就显示采购审批表
    @RequiresPermissions("/api/admin/purchase")
    @GetMapping("/api/admin/purchase/{size}/{page}/{id}")
    public Result listPurchaseByWord(@PathVariable("size") int size, @PathVariable("page") int page,
                                  @PathVariable("id") String id) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+id);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        if(examineService.selectExaminePagebyWord(pages,id,1)==null){
            return ResultFactory.buildFailResult("没有该编号，请重新输入");
        }else {
            return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyWord(pages, id,1));
        }
    }

    //根据状态就显示采购审批表
    @RequiresPermissions("/api/admin/purchase")
    @GetMapping("/api/admin/purchase/{size}/{page}/stat/{status}")
    public Result listPurchaseByStatus(@PathVariable("size") int size, @PathVariable("page") int page,
                                  @PathVariable("status") int stutas) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+stutas);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);

            return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyStatus(pages, stutas,1));
    }
    //删除采购审批表
    @PostMapping("/api/admin/purchase/delete")
    public Result delete(@RequestBody ExamineEntity examineEntity) throws Exception {
        examineService.removeById(examineEntity.getId());
        return ResultFactory.buildSuccessResult("删除成功");
    }
    //批量删除采购审核表
    @PostMapping("/api/admin/purchase/deletes/{ids}")
    public Result deletePurchase(@PathVariable("ids") String ids){
        System.out.println("ids"+ids);
        String[] AdvertiseIds = ids.split("\\,");
        examineService.removeByIds(Arrays.asList(AdvertiseIds));
        return ResultFactory.buildSuccessResult("批量采购审核表删除成功");
    }

    //采购表审核
    @CrossOrigin
    @PutMapping("/api/admin/purchase/status/{username}")
    public Result updateAdvertiseStatus(@RequestBody ExamineEntity requestExamine,@PathVariable("username") String username ) {
        System.out.println("传入的值为："+requestExamine+"执行者为："+username);
        System.out.println("执行者为："+username);
        ExamineEntity examineEntity=examineService.updateExamineStatus(requestExamine,username);
        String message = "审核成功";
        return ResultFactory.buildSuccessResult(message);
    }
}
